package com.ecube.maintenance.biz.commons;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.ecube.maintenance.pojos.BaseInfo;
import com.ecube.maintenance.utils.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * Created by Trible Chen on 2014/9/5.
 * this class is an adapter for Volley Listener
 * here we implement simply these listeners,so that the ui thread can easily use the @ICCallBack
 */
public class VolleyCallbackAdapter<T> implements Response.Listener<String>,Response.ErrorListener{

    ICCallBack<T> callBack;
    Type callbackType;
    ITestDataProvider provider;

    public VolleyCallbackAdapter(ICCallBack<T> callBack,Type type,ITestDataProvider... providers){
        this.callBack = callBack;
        callbackType = type;
        if ( providers != null && providers.length > 0 ){
            provider = providers[0];
        }
    }

    /**
     * this method will be call when volley get Error while do request
     * @param volleyError
     */
    @Override
    public void onErrorResponse(VolleyError volleyError) {
        ICTException exception = new ICTException(volleyError.getMessage(),volleyError);
        if ( volleyError.networkResponse != null ){
            exception.setMessageCode(volleyError.networkResponse.statusCode);
            exception.setMessage(new String(volleyError.networkResponse.data));
        } else {
//            String error = volleyError.toString();
//            int idx = error.lastIndexOf(".");
//            if ( idx != -1 ){
//                error = error.substring(idx);
//            }
            exception.setMessage("连接网络失败,请重试。");
            exception.setMessageCode(-1);
        }

        if (callBack != null)
        callBack.done(null,exception);
    }

    /**
     *      this method will be call when volley get response from server
     * @param o the result from network.
     *          this String will be parsed by Gson
     */
    @Override
    public void onResponse(String o) {
        if ( provider != null ){
            o = provider.getTestData();
        }
        BaseInfo<T> info = GsonUtil.getInfosFromJson(o,callbackType);
        ICTException exception = null;

        //check weather error exist in response,convert error response to exception
        if ( info != null && info.getErrorCode() != ICTException.E_APP_OK ){
            exception = new ICTException(info.getErrorMsg());
            exception.setMessageCode(info.getErrorCode());
        } else if ( info == null ){
            exception = new ICTException(new IllegalStateException().getMessage());
            exception.setMessageCode(ICTException.E_APP_REQDATAISNULL);
        }
        if (callBack != null){
            if ( info == null ){
                callBack.done(null,exception);
            } else {
                callBack.done(info.getReturnData(),exception);
            }
        }
    }
}
